//
//  YZShadowView.m
//  shadow
//
//  Created by Pro on 2022/1/14.
//

#import "YZShadowView.h"
@interface YZShadowView()
@property(nonatomic,strong)NSArray * yzPoints;
@property(nonatomic,strong)UIColor * yzColor;
@end

@implementation YZShadowView

- (instancetype)initWithFrame:(CGRect)frame points:(NSArray *)points Color:(UIColor *)color {
    self = [super initWithFrame:frame];
    self.yzPoints = points;
    self.yzColor = color;
    if (self) {
        self.backgroundColor = [UIColor systemGroupedBackgroundColor];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, self.yzColor.CGColor);
    CGContextSetLineWidth(context, 1.5);//线的宽度

    CGPoint sPoints[self.yzPoints.count];//坐标点
    for (int i = 0; i < self.yzPoints.count; i ++) {
        NSValue * value = self.yzPoints[i];
        CGPoint point = [value CGPointValue];
            sPoints[i] = point;
    }
    CGContextAddLines(context, sPoints, self.yzPoints.count);//添加线
//    CGContextClosePath(context);//封起来
//    CGContextSetLineCap(context, kCGLineCapButt);
    CGContextDrawPath(context, kCGPathStroke); //根据坐标绘制路径
    
    for (int i = 0; i < self.yzPoints.count - 1; i ++) {
//        给矩形添加阴影
        NSValue * value = self.yzPoints[i];
        CGPoint point = [value CGPointValue];

        NSValue * value1 = self.yzPoints[i + 1];
        CGPoint point1 = [value1 CGPointValue];
        
        CGPoint  pointzero = CGPointMake(point.x, 240);
        
        CGPoint point3 = CGPointMake(point1.x, 240);
        
        CGPoint sPoints[4];//坐标点
        sPoints[0] = pointzero;
        sPoints[1] = point;
        sPoints[2] = point1;
        sPoints[3] = point3;

        CGContextAddLines(context, sPoints, 4);//添加线

        CGContextSetStrokeColorWithColor(context, [UIColor clearColor].CGColor);
        CGContextSetLineWidth(context, 0);//线的宽度
        CGContextClosePath(context);//封起来
        CGContextSetFillColorWithColor(context, [_yzColor colorWithAlphaComponent:0.1].CGColor);//填充颜色
        CGContextDrawPath(context, kCGPathFillStroke);


//        CGContextDrawPath(context, kCGPathStroke); //根据坐标绘制路径

    }
    
}

@end
